﻿using Shiloh.Job.Model.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
namespace Shiloh.Job.Repositories
{
    public class LogRepo
    {
        private Repository<ActionLog> _repository;
        public LogRepo(Repository<ActionLog> repository)
        {
            _repository = repository;
        }

        public Task<bool> AddActionLog(ActionLog actionLog)
        {
            return _repository.InsertAsync(actionLog);
        }

        /// <summary>
        /// 一定时间内输错账号的次数
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public Task<int> GetLoginFailCountInMin(string username,int minutes)
        {
            return _repository.CountAsync(it =>
            it.Username != null
            && it.Username.Equals(username)
            && SqlFunc.ToInt32(it.ActionData) == 0
            && SqlFunc.DateDiff(DateType.Minute, it.ActionTime, DateTime.Now) <= minutes);
        }
    }
}
